app.service('clientService',['$q','utilService',function($q,utilService){

    //获取该商户所有会员卡名称
    this.getMemberCardAll=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'memberCard/name/all',
            params:$scope.memberCardVO
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    this.confirm = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'client/add',
            data:$scope.memberClient,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.updateCard = function($scope){
        if($scope.clientUpdate.clientStatus==0){
            delete $scope.clientUpdate.memberCardId;
        }
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'client/update',
            data:$scope.clientUpdate,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    //获取该客户的信息
    this.findClientById=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'client/find',
            params:{clientId:$scope.clientUpdate.id}
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    //查找最大值及取值范围
    this.findValue=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'client/value'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    this.charge = function($scope){
        if($scope.clientCharge.isGiveIntegral==0){
            delete $scope.clientCharge.integral;
        }
        if($scope.clientCharge.isGiveCoupons==0){
            delete $scope.clientCharge.couponsId;
        }
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'client/charge',
            data:$scope.clientCharge,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.reservedNum = function($scope){
        var startValue="";
        var endValue="";
        for(var i=0;i<6-$scope.reservedNumber.start.toString().length;i++){
            startValue+="0";
        }
        for(var i=0;i<6-$scope.reservedNumber.end.toString().length;i++){
            endValue+="0";
        }
        startValue=startValue+$scope.reservedNumber.start.toString();
        endValue=endValue+$scope.reservedNumber.end.toString();
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'client/reserved',
            data:{startValue:startValue,endValue:endValue},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.findCoupons=function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'coupons/list',
            params:$scope.coupons
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };

    this.give = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'client/give',
            data:{userId:$scope.coupons.id,couponsId:$scope.coupons.couponsId},
            method:'POST'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    };

    this.checkType = function($scope){
        var deferred = $q.defer();
        utilService.get({
            url:$scope.app.domain + 'shop/check'
        }).then(function(response){
            deferred.resolve(response.data);
        },function(e){
            console.error(e);
            deferred.reject("连接服务器出错");
        });
        return deferred.promise;
    };
}]);